//
//  LoginViewModel.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/22.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "LoginViewModel.h"
#import "SecurityUtil.h"
@implementation LoginViewModel

#pragma mark - NetRequestClass 普通的网络请求

+(void)oprationClient:(NSDictionary *)registDict type:(NSString *)type success:(void (^)(void))successBlock {
    
    //type 1 登录 0 注册
    
    NSString *showStatus = @"注册成功";
    NSString *urlString = UserRigister;
    if([type boolValue]){
        showStatus = @"登录成功";
        urlString = UserLogin;
    }
    //加密
    NSString *tokenValue = [SecurityUtil dictionaryToJson:registDict];
    NSDictionary *params = @{@"token":tokenValue};
    
    [NetRequestClass NetRequestPOSTWithRequestURL:urlString WithParameter:params WithReturnValeuBlock:^(id returnValue) {
        
        if ([returnValue[@"code"] intValue] == 200){
            [SVProgressHUD showSuccessWithStatus:showStatus];
            //存入用户信息
            [self saveUserInfo:returnValue withType:type];
            
            double delayInSeconds = 0.3;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                successBlock();
            });
            
        }else{
            [SVProgressHUD showInfoWithStatus:returnValue[@"msg"]];
        }
    } WithErrorCodeBlock:^(id errorCode) {
        [SVProgressHUD showInfoWithStatus:NET_WORK_ERROR];
        
    } WithFailureBlock:^{
        [SVProgressHUD showInfoWithStatus:NET_WORK_ERROR];
    }];
}

//存入用户信息
+ (void)saveUserInfo:(id)userInfo withType:(NSString *)type{
    
    Account *account = [[Account alloc] init];

    account.userId = userInfo[@"userid"];
    
    if([type boolValue]){
    
    }else{
       account.phone = userInfo[@"username"];   //手机号
    }
    
    
   [DMAccountTool saveAccount:account];
}

+(void)getAuthCodeSuccess:(void (^)(void))successBlock {
    [NetRequestClass NetRequestPOSTWithRequestURL:AuthCode WithParameter:nil WithReturnValeuBlock:^(id returnValue) {
        //        if(returnValue){
        //
        //        }
        //暂时没有网络请求 TO DO..
        [SVProgressHUD showSuccessWithStatus:@"验证码发送成功, 请注意查收"];
        successBlock();
        DMLog(@"%@",returnValue);
    } WithErrorCodeBlock:^(id errorCode) {
        [SVProgressHUD showInfoWithStatus:NET_WORK_ERROR];
    } WithFailureBlock:^{
        [SVProgressHUD showInfoWithStatus:NET_WORK_ERROR];
    }];
    
}
+ (void)resetPassword:(NSDictionary *)valueDict success:(void (^)(void))successBlock {
    //加密
    NSString *tokenValue = [SecurityUtil dictionaryToJson:valueDict];
    NSDictionary *params = @{@"data":tokenValue};
    
    [NetRequestClass NetRequestPOSTWithRequestURL:ResetPassword WithParameter:params WithReturnValeuBlock:^(id returnValue) {
        
        if([returnValue[@"code"] intValue]== 200) {
            [SVProgressHUD showInfoWithStatus:@"密码找回成功"];
        }else{
            [SVProgressHUD showInfoWithStatus:returnValue[@"msg"]];
        }
        
    } WithErrorCodeBlock:^(id errorCode) {
        [SVProgressHUD showInfoWithStatus:NET_WORK_ERROR];

    } WithFailureBlock:^{
        [SVProgressHUD showInfoWithStatus:NET_WORK_ERROR];

    }];
}
#pragma 对ErrorCode进行处理
-(void) errorCodeWithDic: (NSDictionary *) errorDic
{
    self.errorBlock(errorDic);
}

#pragma 对网路异常进行处理
-(void) netFailure
{
    self.failureBlock();
}
@end
